<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-08-26
 * Time: 19:29
 */
namespace app\common\taglib;
use think\template\TagLib;

class Cxbs extends TagLib{
    /**
     * 定义标签列表
     */
    protected $tags   =  [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'contlist'     => ['attr' => 'name,map,order,nums,listdb,keys,unsz', 'close' => 1], //闭合标签，默认为不闭合
        'artilist'     => ['attr' => 'name,map,mid,order,nums,listdb', 'close' => 1], //闭合标签，默认为不闭合
        'tuangoulist'     => ['attr' => 'name,pinclass,fid,order,nums,listdb', 'close' => 1], //闭合标签，默认为不闭合
        'count'      => ['attr' => 'name,map', 'close' => 0],

    ];

    /**
     * @param $tag  要筛选的数据
     * @param $cont 模板内容
     * @return string 正常输出
     */
    public function tagContlist($tag,$cont){
        if(empty($tag['name'])){
            return '******标签缺少命名*******';
        }
        $name = $tag['name'];   //  表名
        $map = $tag['map'];     //  字段
        $order = empty($tag['order']) ? null : $tag['order'];     // 排序
        $nums = empty($tag['nums']) ? 12 : $tag['nums'];     // 数量
        $listdb = $tag['listdb'];       //  变量值
        $keys = empty($tag['keys']) ? 'cx' : $tag['keys'];     // 索引值
        $unsz = empty($tag['unsz']) ? null : $tag['unsz'];      //  需要解压的字段
        //  开始执行标签
        $parse = '<?php ';
        $parse .= '$'.$listdb."= model('".$name."')->where(".$map.")->where('status','1')";
        if(!empty($order)){
            $parse .= '->order('.$order.')';
        }
        if(!empty($nums)){
            $parse .= '->limit('.$nums.')';
        }
        $parse .= "->select();";
        if(!empty($unsz)){
            $parse .= 'foreach ($'.$listdb.' as $k => $v):';
            $parse .= '$v[\''.$unsz.'\'] = unserialize($v[\''.$unsz.'\']);';
            $parse .= '$'.$listdb.'[$k] = $v;';
            $parse .= 'endforeach;';
        }
        $parse .= '?>';
        $parse .= $cont;
        return $parse;
    }

    /**
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagArtilist($tag,$content){
        if(empty($tag['name'])){
            return '******标签缺少命名*******';
        }
        $name = $tag['name'];   //  表名
        $map = $tag['map'];     //  字段
        $mid = empty($tag['mid']) ? 0 : $tag['mid'];     // 排序
        $order = empty($tag['order']) ? null : $tag['order'];     // 排序
        $nums = empty($tag['nums']) ? 12 : $tag['nums'];     // 数量
        $listdb = $tag['listdb'];       //  变量值
        $parse = '<?php ';
        $parse .= '$'.$listdb."= model('Article')->where(".$map.")->where('status','1')";
        if($mid != '0'){
            $parse .= "->where('mid','".$mid."')";
        }
        if(!empty($order)){
            $parse .= '->order('.$order.')';
        }
        if(!empty($nums)){
            $parse .= '->limit('.$nums.')';
        }
        $parse .= "->select();";
        $parse .= '$cxModel = new \app\common\model\Article;';
        $parse .= 'foreach ($'.$listdb.' as $k => $v):';
        $parse .= '$'.$listdb.'[$k] = $cxModel->readcont($v);';
        $parse .= 'endforeach;';
        $parse .= '?>';
        $parse .= $content;
        return $parse;
    }
    public function tagTuangoulist($tag,$content){
        if (empty($tag['name'])) {
            return '******标签缺少命名*******';
        }
        $pinclass =  empty($tag['pinclass']) ? null : $tag['pinclass'];      //  团购类型
        $fid = empty(request()->param('fid')) ? null : request()->param('fid');     // 调用栏目
        $order = empty($tag['order']) ? null : $tag['order'];     // 排序
        $nums = empty($tag['nums']) ? 24 : $tag['nums'];     // 数量
        $listdb = $tag['listdb'];       //  变量值
        $parse = '<?php ';
        $parse .= '$'.$listdb."= db('article')->alias('a')->join('article_content_101 b','b.aid = a.aid')->where('a.status','1')->where('b.kstims','<',time())->where('b.jietims','>',time())";
        if(!empty($pinclass)){
            $parse .= "->where('b.pinclass','".$pinclass."')";
        }
        if(!empty($fid)){
            $parse .= "->where('a.fid',".$fid.")";
        }
        $parse .= "->paginate(".$nums.",false,['query'=>request()->param()]);";
        $parse .= '$pages = $'.$listdb.'->render();';
//        $parse .= '->limit('.$nums.')->select();';
        $parse .= '$cxModel = new \app\common\model\Article; $webdb = webdb();';
        $parse .= 'foreach ($'.$listdb.' as $k => $v){';
        $parse .= "if(!empty(\$v['pinimg'])){ \$v['pinimg'] = imgeditadd(\$v['pinimg'],false);}";
        $parse .= "if(empty(\$v['picurl'])){ \$v['picurl'] = model('Article')->where('aid',\$v['paid'])->value('picurl');}";
        $parse .= "\$v['shichangjia'] =db('article_content_100')->where('aid',\$v['paid'])->value('shichuangjia');";
        $parse .= "if(\$v['pinclass'] == '0元团'){ \$v['pinmoney'] = '0';}";
        $parse .= "\$v['addnum'] = model('Order')->where('aid',\$v['aid'])->count();";
        $parse .= "\$picurl = explode('/',\$v['picurl']);";
        $parse .= "\$v['picurl3'] = '/'.\$webdb['updir'].'/'.\$v['picurl'];";
        $parse .= "\$v['picurl'] = '/'.\$webdb['updir'].'/'.\$picurl['0'].'/1x1_'.\$picurl['1'];";
        $parse .= "\$v['picurl1'] = '/'.\$webdb['updir'].'/'.\$picurl['0'].'/3x4_'.\$picurl['1'];";
        $parse .= "\$v['picurl2'] = '/'.\$webdb['updir'].'/'.\$picurl['0'].'/4x3_'.\$picurl['1'];";
        $parse .= '$'.$listdb.'[$k] = $v;';
        $parse .= '}';
        $parse .= ' ?>';
        $parse .= $content;
        return $parse;

    }

}
